<template>
  <div class="nav">
    <div class="left">
      <a
        class="nav_home"
        @click="goHome"
        v-if="$router.currentRoute.value.path !== '/login'"
        >返回首页</a
      >
    </div>
    <div class="right">
      <a-button
        shape="round"
        danger
        :icon="h(PoweroffOutlined)"
        @click="to_logout()"
      >
        退出登录
      </a-button>
    </div>
  </div>
</template>

<script setup>
import { h, onMounted } from "vue";
import { PoweroffOutlined } from "@ant-design/icons-vue";
import { useRouter } from "vue-router";
import { logout } from "@/api/user.js";
import { message } from "ant-design-vue";

const $router = useRouter();

onMounted(() => {
  // console.log($router.currentRoute.value.path);
});

const goHome = () => {
  $router.push("/");
};

const to_logout = async () => {
  try {
    let res = await logout();
    console.log("res ->", res);
    if (res.code === "sso.ok") {
      message.success("退出成功");
    } else {
      message.error("退出登录失败");
    }
  } catch (error) {
    console.error(error);
  }
  clear_user_info();
  $router.push("/login");
};
</script>

<script>
import { Button } from "ant-design-vue";
import { clear_user_info } from "@/utils/storage";

export default {
  components: {
    AButton: Button,
  },
};
</script>

<style lang="scss" scoped>
.nav {
  background-color: #ffffff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 24px 20px;
  align-items: center;
  max-height: 50px;
  display: flex;
  flex-direction: row;
  justify-content: space-between;

  .left {
    display: flex;
    align-items: center;
    justify-content: space-between;

    .nav_logo {
      font-weight: bold;
      font-size: 1.2rem;
      color: #333333;
      width: 200px;
    }

    .nav_home {
      cursor: pointer;
    }
  }
}
</style>
